package org.example.banner.service.Impl;

import cn.humingfeng.dynamic.datasource.annotation.DS;
import org.example.entity.Base.DataSourceContextHolder;
import org.example.banner.entity.dto.Bannerdto;
import org.example.banner.entity.vo.Bannervo;
import org.example.banner.repository.BannerRepository;
import org.example.banner.service.BannerService;
import org.example.util.FileSetUtil;
import org.example.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.sql.Timestamp;
import java.time.LocalTime;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    private final BannerRepository bannerRepository;

    public BannerServiceImpl(BannerRepository bannerRepository) {
        this.bannerRepository = bannerRepository;
    }

    @Override
   public List<Bannervo> getList(){
        return bannerRepository.getList();
    }
    @Override
    public Bannervo logo(){
        DataSourceContextHolder.setDataSource("cassellbanner");
        return bannerRepository.logo();
    }
    @Override
    public List<Bannervo> MainBanner(){
        return bannerRepository.MainBanner();
    }
    @Override
    public  int addBanner(MultipartFile image, String name, Integer type){
        File file =  FileSetUtil.saveFile(image,"./external-resources/", LocalTime.now().toString());
        String imageurl= OssUtil.uploadImage(file.getName());
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// 设置纳秒部分，保留最多6位微秒
        timestamp.setNanos(timestamp.getNanos() / 1000 * 1000);
        Bannerdto banner=new Bannerdto(null,imageurl, timestamp.toString(),name,type);
        return bannerRepository.addBanner(banner);
    }
}
